import { http } from "@/utils/http";
import { baseUrlApi } from "./utils";

export type UserResult = {
  code: any;
  success: boolean;
  data: {
    [x: string]: any;
    avatar: string;
    username: string;
    nickname: string;
    roles: Array<string>;
    permissions: Array<string>;
    accessToken: string;
    refreshToken: string;
    expires: Date;
  };
};
// type allCustomerResult = {
//   success: boolean;
//   data?: Array<any>;
// };
type allUserResult = {
  success: boolean;
  data: {
    records: Array<any>;
    total: number;
    size: number;
    current: number;
    pages: number;
  };
};
export type RefreshTokenResult = {
  success: boolean;
  data: {
    /** `token` */
    accessToken: string;
    /** 用于调用刷新`accessToken`的接口时所需的`token` */
    refreshToken: string;
    /** `accessToken`的过期时间（格式'xxxx/xx/xx xx:xx:xx'） */
    expires: Date;
  };
};

/** 登录 */
export const getLogin = (data?: object) => {
  // return http.request<UserResult>("post", baseUrlApi("consumer/user/login"), {
  return http.request<UserResult>("post", baseUrlApi("user/login"), {
    data
  });
};
export type addCustomerResult = {
  code: any;
  success: boolean;
  data?: Array<any>;
};
type deleteCustomerResult = {
  code: any;
  msg: Array<any>;
  data?: Array<any>;
};
/** 刷新`token` */
export const refreshTokenApi = (data?: object) => {
  return http.request<RefreshTokenResult>("post", baseUrlApi("refresh-token"), {
    data
  });
};
// 默认进来查找所有的用户
export const getAllUesrList = (data?: object) => {
  return http.request<allUserResult>("post", baseUrlApi("user/list "), {
    data
  });
};

// 添加用户
export const addUser = (data?: object) => {
  return http.request<addCustomerResult>("post", baseUrlApi("user/add"), {
    data
  });
};
// 修改用户信息
export const updateUser = (data?: object) => {
  return http.request<deleteCustomerResult>("post", baseUrlApi("user/update"), {
    data
  });
};
// 删除当前行用户
export const deleteCurrentUser = (data?: object) => {
  return http.request<deleteCustomerResult>("post", baseUrlApi("user/delete"), {
    data
  });
};
// 修改用户密码
export const changePassword = (data?: object) => {
  return http.request<deleteCustomerResult>(
    "post",
    baseUrlApi("consumer/user/updatePassword"),
    {
      data
    }
  );
};
// 修改用户密码
export const resetPassword = (data?: object) => {
  return http.request<deleteCustomerResult>(
    "post",
    baseUrlApi("user/resetPassword"),
    {
      data
    }
  );
};
